package com.example.city_home_app.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.city_home_app.Bean.Down_NewsReply;
import com.example.city_home_app.Bean.Down_Universe;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_NewsComment;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_NewsReply;
import com.example.city_home_app.RetrofitListener;
import com.example.city_home_app.UI.MyCircleImageView;
import com.example.city_home_app.UI.UIUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.shaohui.bottomdialog.BottomDialog;

public class NewsReplyActivity extends BaseActivity {
    @BindView(R.id.img_newreply_comment)
    ImageView imgNewreplyComment;
    @BindView(R.id.img_newreply_like)
    ImageView imgNewreplyLike;
    @BindView(R.id.img_newreply_back)
    ImageView imgNewreplyBack;
    @BindView(R.id.tv_newsreply_title)
    TextView tvNewsreplyTitle;
    @BindView(R.id.linearLayout3)
    LinearLayout linearLayout3;
    @BindView(R.id.img_newreply_head)
    MyCircleImageView imgNewreplyHead;
    @BindView(R.id.tv_newreply_name)
    TextView tvNewreplyName;
    @BindView(R.id.tv_newreply_content)
    TextView tvNewreplyContent;
    @BindView(R.id.tv_newreply_time)
    TextView tvNewreplyTime;
    @BindView(R.id.linearLayout4)
    LinearLayout linearLayout4;
    @BindView(R.id.tv_newreply_like)
    TextView tvNewreplyLike;
    @BindView(R.id.tv_newreply_count)
    TextView tvNewreplyCount;
    @BindView(R.id.linearLayout5)
    LinearLayout linearLayout5;
    @BindView(R.id.rec_news_reply)
    RecyclerView recNewsReply;

    private int commentid,newsid,like,count,authorid;
    private String content,imgurl,username,time;
    private ApiModel model = new ApiModel();
    private Adapter_NewsReply adapter_newsReply;
    private boolean isLike = false;
    private BottomDialog dialog;

    private RetrofitListener<List<Down_NewsReply>> replylistretrofitListener = new RetrofitListener<List<Down_NewsReply>>() {
        @Override
        public void onResponse(List<Down_NewsReply> listener) {
            count = listener.size();
            tvNewreplyCount.setText(count+"条回复");
            adapter_newsReply.setList(listener);
            adapter_newsReply.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> islikeretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                isLike = true;
                imgNewreplyLike.setImageResource(R.drawable.ic_like);
            }else{
                imgNewreplyLike.setImageResource(R.drawable.ic_like_no);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> likeretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(NewsReplyActivity.this,"点赞成功",UIUtils.LENGTH_SHORT);
                imgNewreplyLike.setImageResource(R.drawable.ic_like);
                if(like==0){
                    tvNewreplyLike.setVisibility(View.VISIBLE);
                }
                like++;
                tvNewreplyLike.setText(""+like);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> unlikeretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(NewsReplyActivity.this,"取消成功",UIUtils.LENGTH_SHORT);
                imgNewreplyLike.setImageResource(R.drawable.ic_like_no);
                if(like==1){
                    tvNewreplyLike.setVisibility(View.INVISIBLE);
                }
                like--;
                tvNewreplyLike.setText(""+like);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    private RetrofitListener<Down_Universe> replyretrofitListener = new RetrofitListener<Down_Universe>() {
        @Override
        public void onResponse(Down_Universe listener) {
            if(listener.getMessage().equals("success")){
                UIUtils.showToast(NewsReplyActivity.this,"回复成功",UIUtils.LENGTH_SHORT);
                adapter_newsReply.clearList();
                model.getReplyNewComment(commentid,replylistretrofitListener);
            }
        }

        @Override
        public void onFail(String msg) {

        }
    };

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_news_reply;
    }

    @Override
    protected void initViews() {
        super.initViews();
        ButterKnife.bind(this);
        recNewsReply = (RecyclerView)findViewById(R.id.rec_news_reply);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recNewsReply.setLayoutManager(layoutManager);
        adapter_newsReply = new Adapter_NewsReply(this,getSupportFragmentManager());
        recNewsReply.setAdapter(adapter_newsReply);
        recNewsReply.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    protected void initData() {
        super.initData();
        Intent intent = getIntent();
        commentid = intent.getIntExtra("id",0);
        content = intent.getStringExtra("content");
        authorid = intent.getIntExtra("authorid",0);
        imgurl = intent.getStringExtra("img");
        username = intent.getStringExtra("username");
        time = intent.getStringExtra("time");
        like = intent.getIntExtra("like",0);
        newsid = intent.getIntExtra("newsid",0);
        tvNewreplyContent.setText(content);
        if(like != 0) {
            tvNewreplyLike.setText("" + like);
        }else{
            tvNewreplyLike.setVisibility(View.INVISIBLE);
        }
        tvNewreplyName.setText(username);
        tvNewreplyTime.setText(time);
        Picasso.with(this)
                .load("http://siminzhang.top:8088/city_home_server/public/static/posters/"+imgurl)
                .into(imgNewreplyHead);

        model.getReplyNewComment(commentid,replylistretrofitListener);
        model.islikenewcomment(BaseActivity.getUserinfo().getUserid(),commentid,islikeretrofitListener);
    }

    @OnClick({R.id.img_newreply_comment, R.id.img_newreply_like, R.id.img_newreply_back,R.id.img_newreply_head})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_newreply_comment:
                dialog = BottomDialog.create(getSupportFragmentManager());
                dialog.setViewListener(new BottomDialog.ViewListener() {
                    @Override
                    public void bindView(final View v) {
                        final EditText mEditText = (EditText)v.findViewById(R.id.edittext_dialog_newscomment_content);
                        Button button = (Button)v.findViewById(R.id.btn_dialog_newscomment_send);
                        mEditText.post(new Runnable() {
                            @Override
                            public void run() {
                                InputMethodManager imm =
                                        (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                                imm.showSoftInput(mEditText, 0);
                            }
                        });
                        button.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                if(!mEditText.getText().toString().equals("")){
                                    dialog.dismiss();
                                    model.replynewcomment(newsid,commentid,BaseActivity.getUserinfo().getUserid(),mEditText.getText().toString(),replyretrofitListener);
                                }else{
                                    UIUtils.showToast(NewsReplyActivity.this,"格式非法",UIUtils.LENGTH_SHORT);
                                }
                            }
                        });
                    }
                })
                        .setLayoutRes(R.layout.newscomment_dialog)
                        .setDimAmount(0.1f)
                        .setCancelOutside(true)
                        .setTag("评论")
                        .show();
                break;
            case R.id.img_newreply_like:
                if(isLike){
                    model.unlikenewcomment(BaseActivity.getUserinfo().getUserid(),commentid,newsid,unlikeretrofitListener);
                    isLike = false;
                }else{
                    model.likenewcomment(BaseActivity.getUserinfo().getUserid(),commentid,newsid,likeretrofitListener);
                    isLike = true;
                }
                break;
            case R.id.img_newreply_back:
                finish();
                break;
            case R.id.img_newreply_head:
                Intent intent = new Intent(NewsReplyActivity.this,UserInfoActivity.class);
                intent.putExtra("id",authorid);
                startActivity(intent);
                break;
        }
    }
}
